import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:mall/components/image_network.dart';
import 'package:mall/constants.dart';
import 'package:mall/core/models/store_index.dart';
import 'package:mall/core/models/store_index_req.dart';
import 'package:mall/core/services/new_api.dart';
import 'package:mall/screens/home/components/home_banner.dart';
import 'package:mall/screens/home/components/home_floor.dart';
import 'package:mall/screens/home/components/home_rush.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

import 'store_group.dart';

class StoreHome extends StatefulWidget {
  final int storeId;

  const StoreHome({
    Key key,
    @required this.storeId,
  }) : super(key: key);

  @override
  _StoreHomeState createState() => _StoreHomeState();
}

class _StoreHomeState extends State<StoreHome> {
  String _error;
  StoreIndex _storeIndex;

  @override
  void initState() {
    super.initState();
    _fetchStoreIndex();
  }

  @override
  Widget build(BuildContext context) {
    return _error == null && _storeIndex == null
        ? Center(
            child: CircularProgressIndicator(),
          )
        : _storeIndex == null
            ? Center(
                child: Text("$_error"),
              )
            : buildContent();
  }

  SingleChildScrollView buildContent() {
    return SingleChildScrollView(
      child: Column(
        children: [
          _buildTitle(),
          if (_storeIndex.bannerList.isNotEmpty) HomeBanner(banner: _storeIndex.bannerList),
          SizedBox(height: 8.w),
          StoreGroup(group: _storeIndex.pinGouList),
          SizedBox(height: 8.w),
          HomeRush(rush: _storeIndex.qiangGouList),
          SizedBox(height: 8.w),
          HomeFloor(floor: _storeIndex.floorList),
          SizedBox(height: 8.w),
        ],
      ),
    );
  }

  Widget _buildTitle() {
    return Padding(
      padding: EdgeInsets.all(8.w),
      child: Column(
        children: [
          Row(
            children: [
              ImageNetwork(url: "${_storeIndex.store.logoUrl}", width: 30.w, height: 30.w),
              SizedBox(width: 8.w),
              Text("${_storeIndex.store.name}"),
            ],
          ),
          SizedBox(height: 8.w),
          Row(
            children: [
              SizedBox(width: (30 + 8).w),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 4.w),
                decoration: BoxDecoration(
                  color: Color(0xFFFCAC48),
                  borderRadius: BorderRadius.circular(10.w),
                ),
                child: Text(
                  "平台总店",
                  style: TextStyle(color: Colors.white, fontSize: 10.sp),
                ),
              ),
              SizedBox(width: 8.w),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 4.w),
                decoration: BoxDecoration(
                  color: Color(0xFF298CF6),
                  borderRadius: BorderRadius.circular(10.w),
                ),
                child: Text(
                  "业",
                  style: TextStyle(color: Colors.white, fontSize: 10.sp),
                ),
              ),
              SizedBox(width: 8.w),
            ],
          ),
        ],
      ),
    );
  }

  void _fetchStoreIndex() async {
    final api = context.read<NewApi>();
    try {
      final req = StoreIndexReq(
        clientId: clientId,
        dataTypes: ['ALL'],
        storeId: widget.storeId,
      );
      final storeIndex = await api.storeIndex(req);
      if (!mounted) {
        return;
      }
      setState(() {
        _storeIndex = storeIndex;
      });
    } on DioError catch (e) {
      if (!mounted) {
        return;
      }
      setState(() {
        _error = e.message;
      });
    }
  }
}
